package com.huawei.petSystemdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.huawei.petSystemdemo.R;
import com.huawei.petSystemdemo.bean.Order;
import com.huawei.petSystemdemo.bean.Pets;
import com.huawei.petSystemdemo.bean.Shop;
import com.huawei.petSystemdemo.data.OrderRepository;
import com.huawei.petSystemdemo.data.ShopRepository;

import org.jetbrains.annotations.NotNull;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.OrderViewHolder> {

    private List<Order> orderList = new ArrayList<>();
    private Context context;

    public OrderAdapter(List<Order> orderList, Context context) {
        this.orderList = orderList;
        this.context = context;
    }

    public class OrderViewHolder extends RecyclerView.ViewHolder {
        RecyclerView orderRecycle;
        TextView order_date;
        public OrderViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            orderRecycle = itemView.findViewById(R.id.orderRecycle_1);
            order_date = itemView.findViewById(R.id.order_date);
            context = itemView.getContext();
            LinearLayoutManager manager = new LinearLayoutManager(context);
            orderRecycle.setLayoutManager(manager);
        }
    }
    @NonNull
    @NotNull
    @Override
    public OrderAdapter.OrderViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.comment_order, parent, false);
        return new OrderAdapter.OrderViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull OrderAdapter.OrderViewHolder holder, int position) {

        Order order = orderList.get(position);
        DateFormat bf = new SimpleDateFormat("yyyy-MM-dd E a HH:mm");
        holder.order_date.setText(bf.format(order.getDates()));

        OrderRepository.getPetByOrder(order.getId(), new Consumer<List<Pets>>() {
             @Override
            public void accept(List<Pets> pets) {

                List<Integer> idList = new ArrayList<>();
                boolean flag = true;
                for(Pets pet1 : pets) {
                    idList.add(pet1.getShopId());
                }
                idList = idList.stream().distinct().collect(Collectors.toList());
                List<List<Pets>> lists = new ArrayList<>();
                for (int i = 0; i < idList.size(); i++) {
                    List<Pets> list = new ArrayList<>();
                    for(Pets pets1 : pets) {
                        if(pets1.getShopId() == idList.get(i))
                            list.add(pets1);
                    }
                    lists.add(list);
                }

                //寻找订单中相同商店的商品
                OrderShopAdapter adapter = new OrderShopAdapter(lists, holder.itemView.getContext(), idList , order.getState());
                holder.orderRecycle.setAdapter(adapter);
            }
        });
    }

    @Override
    public int getItemCount() {
        return orderList.size();
    }
}
